import type {App} from "vue";
import ElementPlus from 'element-plus'
// 确保导入Element Plus的样式
import 'element-plus/dist/index.css'
import {$channel} from "./tools/sockets.ts";
import 'font-awesome/css/font-awesome.css'
// 导入组件库自身的基础样式
import './base.css'
import type {MorOption} from "./types/io.types.ts";
import type {MorghulisOptions} from "./types/channel.types.ts";
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import MDialog from "./components/dialog/MDialog.vue";
import MCell from "./components/cell/MCell.vue";
import MTable from "./components/table/MTable.vue";
import MTableButtons from "./components/table/MTableButtons.vue";
import MTableHeader from "./components/table/MTableHeader.vue";
import MButton from "./components/button/MButton.vue";
import {library} from '@fortawesome/fontawesome-svg-core';
import {fas} from '@fortawesome/free-solid-svg-icons'
import {FontAwesomeIcon} from '@fortawesome/vue-fontawesome';
// 导入Element Plus的组件类型，确保IDE能够识别
import type * as ElementPlusComponents from 'element-plus'
// 导入hooks
import {useMorghulisCookies} from "./hooks/cookies.ts";
import {useMorghulisAuthorize} from "./hooks/authorize.ts";
import {useMorghulisChannel} from "./hooks/channel.ts";
import {useMorghulisRequest} from "./hooks/request.ts";
// 导入工具
import {$message, $alert, $confirm} from "./tools/feedback.ts";

// 注册FontAwesome图标
library.add(fas);

// 创建Morghulis实例的工厂函数
export const createMorghulis = (options?: MorOption & MorghulisOptions) => {
    const channel = $channel(options || {})
    const {baseURL} = options || {baseURL: '/api/', minioURL: '/dfs/'}
    useMorghulisChannel(baseURL as string)
    
    return {
        install: (Vue: App) => {
            Vue.provide('options', options)
            Vue.provide('channel', channel)
            Vue.component('MDialog', MDialog)
            Vue.component('MCell', MCell)
            Vue.component('MTable', MTable)
            Vue.component('MTableButtons', MTableButtons)
            Vue.component('MTableHeader', MTableHeader)
            Vue.component('MButton', MButton)
            Vue.use(ElementPlus, {
                locale: zhCn,
            })
            Vue.component('fa', FontAwesomeIcon)
        }
    }
}

// 明确导出组件
export const Button = MButton;

export {
    MDialog,
    // 单元格组件
    MCell,
    // 表格组件
    MTable,
    MTableButtons,
    MTableHeader,
    // 按钮组件
    MButton
}

// 导出hooks
export {
    useMorghulisCookies,
    useMorghulisAuthorize,
    useMorghulisChannel,
    useMorghulisRequest
}

// 导出工具
export {
    $message,
    $alert,
    $confirm,
    $channel
}

// 导出Element Plus，确保用户可以访问所有Element Plus组件
export { ElementPlus }

// 导出类型定义
export type * from './types/dialog.types.ts'
export type * from './types/io.types.ts'
export type * from './types/channel.types.ts'
export type * from './types/feedback.types.ts'
export type * from './types/ui.types.ts'
export type * from './types/cell.types.ts'
export type * from './types/button.types.ts'
// 重新导出Element Plus的类型，确保IDE能够识别
export type * from 'element-plus'

// 默认导出
export default {
    install: (app: App, options?: MorOption) => {
        // 先安装Element Plus，确保其组件和样式被正确加载
        app.use(ElementPlus, {
            locale: zhCn,
        })
        // 再安装Morghulis组件
        createMorghulis(options).install(app)
    },
    MDialog,
    MCell,
    MTable,
    MTableButtons,
    MTableHeader,
    MButton
}